package com.cxm.qyyz.base.fragment;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import androidx.annotation.LayoutRes;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.bumptech.glide.Glide;
import com.cxm.qyyz.R;
import com.cxm.qyyz.base.mvp.BaseContract;
import com.cxm.qyyz.utils.AppUtil;
import com.cxm.qyyz.widget.plus.StatusBarPlus;
import com.trello.rxlifecycle4.LifecycleTransformer;
import com.trello.rxlifecycle4.android.FragmentEvent;


import java.util.Objects;

import butterknife.ButterKnife;
import butterknife.Unbinder;
import per.goweii.anylayer.AnyLayer;
import per.goweii.anylayer.Layer;
import per.goweii.anylayer.dialog.DialogLayer;

public abstract class BaseFragment<T extends BaseContract.BasePresenter> extends PermissionFragment<T> {

    private Unbinder unbinder;
    protected Activity mActivity;
//    private StatePlus statePlus;

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        mActivity = activity;
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        //这里调用super的原因是因为在父类中做了attachView()操作
        super.onCreateView(inflater, container, savedInstanceState);
        View view = inflater.inflate(getLayoutId(), container, false);
        //为fragment根布局设置tag
        view.setTag("ContentView");
        //如果有状态栏
        if (supportStatusBar()) {
            view = initStatusBar(view);
        }
        //是否支持状态控制
        if (supportStateController()) {
//            statePlus = new StatePlus();
//            statePlus.setOnStateControllerListener(new StatePlus.OnStateControllerListener() {
//                @Override
//                public void onStateController(int itemType) {
//                    if (itemType == StatePlus.ERROR) {
//                        onReload();
//                    }
//                }
//            });
//            view = statePlus.init(view);
        }
        unbinder = ButterKnife.bind(this, view);
        //注册EventBus
        if (supportEventBus()) {
            registerEventBus();
        }
        //初始化操作
        initEvents(view);
        initEvents();
        return view;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initViews();
    }

    public void initViews() {

    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        releaseEvents();
        if (supportEventBus()) {
            unregisterEventBus();
        }
        unbinder.unbind();
    }

    protected void releaseEvents() {
        onData();
        onDismiss();
    }

    @Override
    public void onShow() {
        showDialog();
    }

    @Override
    public void onDismiss() {
        dismiss();
    }

    @LayoutRes
    protected abstract int getLayoutId();

    protected void initEvents(View v) {
    }

    protected abstract void initEvents();

    protected View initStatusBar(View view) {
        view = StatusBarPlus.setColor(view, Color.WHITE);
        return view;
    }

    protected boolean supportStatusBar() {
        return false;
    }

    protected boolean supportEventBus() {
        return false;
    }

    protected boolean supportStateController() {
        return true;
    }

    protected void registerEventBus() {
    }

    protected void unregisterEventBus() {
    }

    @Override
    public void toast(String message) {
        AppUtil.getInstance().showToast(message);
        dismiss();
    }

    @Override
    public void toast(int resId) {
        AppUtil.getInstance().showToast(resId);
        dismiss();
    }

    @Override
    public void onData() {
    }

    @Override
    public void onLoad() {
    }

    @Override
    public void onError() {
    }

    @Override
    public void onReload() {

    }

    @Override
    public void jumpToLogin() {

    }

    @Override
    public <E> LifecycleTransformer<E> bindToLife() {
        return this.bindUntilEvent(FragmentEvent.DESTROY_VIEW);
    }

    DialogLayer dialogLayer;

    /**
     * dialog
     * date 2021/12/20
     * author yzx
     */
    public void showDialog() {
//        if (null == dialogLayer)
//            dialogLayer = (DialogLayer) AnyLayer.dialog(requireActivity())
//                    .contentView(R.layout.item_progressbar)
//                    .gravity(Gravity.CENTER)
//                    .cancelableOnTouchOutside(false)
//                    .cancelableOnClickKeyBack(false)
//                    .bindData(new Layer.DataBinder() {
//                        @Override
//                        public void bindData(@NonNull Layer layer) {
//                            Glide.with(requireActivity())
//                                    .asGif()
//                                    .load(R.drawable.loading)
//                                    .into((ImageView) Objects.requireNonNull(layer.getView(R.id.ivLoad)));
//                        }
//                    });
//
//        dialogLayer.show();
    }

    /**
     * 取消弹框
     * date 2021/12/20
     * author yzx
     */
    public void dismiss() {
        if (null != dialogLayer) dialogLayer.dismiss();
    }
}
